<?php

namespace App\Http\Controllers\Publik;

use App\Enum\CodeEnum;
use App\Enum\RedisEnum;
use App\Http\Controllers\Api\ApiBaseController;
use App\Services\PublikService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Swoole\NameResolver\Redis;


class IndexController extends ApiBaseController
{
    /**
     * 捐赠排行
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|null
     */
    public function donationTopList(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $result = parent::_cacheableData(
                RedisEnum::R_INDEX_DONATION_TOP_KEY
                ,RedisEnum::R_INDEX_DONATION_TOP_TO
                , (new PublikService)
                ,'donationTopList'
                , 3
                , $request
            );
            return success($result);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 首页的置顶文章列表
     * @return \Illuminate\Http\JsonResponse|null
     */
    public function articleTopList(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $result = parent::_cacheableData(
                RedisEnum::R_INDEX_ARTICLES_TOP_KEY
                ,RedisEnum::R_INDEX_ARTICLES_TOP_TO
                , (new PublikService)
                ,'articleTopList'
                , 5
                , $request
            );
            return success($result);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 文章详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|null
     */
    public function articleDetail(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $result = parent::_cacheableData(
                sprintf(RedisEnum::R_INDEX_ARTICLES_DETAIL_KEY,$request->article_id)
                ,RedisEnum::R_INDEX_ARTICLES_DETAIL_TO
                , (new PublikService)
                ,'articleDetail'
                , $request->article_id
                , $request
            );
            return success($result);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    public function getCityEnabled(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $result = parent::_cacheableData(
                RedisEnum::R_CITY_ENABLED_KEY
                ,RedisEnum::R_CITY_ENABLED_TO
                , (new PublikService)
                ,'getCityEnabled'
                , ''
                , $request
            );
            return success(parent::cityCodeUp($result));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 获取按照拼音首字母进行排序的结果
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|null
     */
    public function getCityEnabledAlpha(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $result = parent::_cacheableData(
                RedisEnum::R_CITY_ENABLED_KEY
                ,RedisEnum::R_CITY_ENABLED_TO
                , (new PublikService)
                ,'getCityEnabled'
                , ''
                , $request
            );
            return success($this->alphaCityEnabled(parent::cityCodeUp($result)));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    public static function getRedisKeys()
    {
        return success(RedisEnum::R_KEYS);
    }
    public static function clearRedisKey(Request $request)
    {
        $redis = app("redis.connection");
        $redis->del($$request->redis_key);
        return success('清除成功');
    }

    /**
     * 将开通的城市 进行格式化
     *
     * @param $rows
     * @return array
     */
    private function alphaCityEnabled($rows): array
    {
        $result = [];
        foreach( $rows as $row ){
            $a = $this->_getFirstPinyin($row['city_pinyin']);
            if( !isset($result[$a]) ) {
                $result[$a] = [
                    'nav'=> $a,
                    'navList'=>[]
                ];
            }
            $result[$a]['navList'][] = $row;
        }
        return array_values($result);
    }
    private function _getFirstPinyin($pinyin)
    {
        if( empty($pinyin) ) return '-';

        return substr(ucfirst($pinyin),0,1);
    }
}
